/* Ekeko - Server based resource loader
 * Copyright (C) 2013 Jorge Luis Zapata
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "Ekeko.h"
#include "ekeko_private.h"
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
static int _init = 0;
static Eina_List *_pending_replies = NULL;
/*----------------------------------------------------------------------------*
 *                            The server interface                            *
 *----------------------------------------------------------------------------*/
/* client callbacks */
static void _client_server_add(Eix_Server *srv, void *socket, void *user_data)
{
}

static void _client_server_del(Eix_Server *src, void *socket, void *user_data)
{
}

static void _client_server_data(Eix_Server *srv, void *socket,
		const Eix_Message *m, void *data,
		void *reply, Eix_Error *err, void *user_data)
{
	ERR("Data received");
	if (m == EKEKO_MSG_IMAGE_LOAD_REPLY)
	{
		ERR("image loaded");
		/* iterate over the list of pending replies to look for the
		 * message received
		 */
	}
}

static Eix_Server_Descriptor _client_descriptor = {
	/* .add 	= */ _client_server_add,
	/* .del 	= */ _client_server_del,
	/* .data 	= */ _client_server_data,
};
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
int ekeko_log;
Eix_Server *ekeko_server = NULL;
/*============================================================================*
 *                                   API                                      *
 *============================================================================*/
EAPI void ekeko_init(void)
{
	Eix_Transport *t;
	Eix_Server *srv;

	if (_init++) return;

	/* eina */
	eina_init();
	ekeko_log = eina_log_domain_register("ekeko", NULL);
	/* eix */
	eix_init();
	ekeko_common_message_init();

	/* try to do the connection */
	t = eix_transport_unix_tcp_new(EKEKO_COMMON_SERVER_PATH);
	/* TODO add sync version of the connect */
	srv = eix_server_connect(t, &_client_descriptor, NULL);
	if (!srv)
	{
		ERR("Failed to connect to ekeko server");
		//eix_transport_free(t);
		ekeko_shutdown();
		return;
	}
	ekeko_server = srv;
	DBG("Connected to the server correctly");

	/* eshm */
	eshm_init();
}

EAPI void ekeko_shutdown(void)
{
	if (_init == 1)
	{
		/* eshm */
		eshm_shutdown();

		/* eix */
		if (ekeko_server)
		{
			eix_server_del(ekeko_server);
		}
		ekeko_common_message_shutdown();
		eix_shutdown();

		/* eina */
		eina_log_domain_unregister(ekeko_log);
		eina_shutdown();
	}
	_init--;
}

/* The ecore code should be conditional as it is only a mandatory dependency
 * for the ekeko bin
 */
EAPI Eina_Bool ekeko_ecore_loopize(void)
{
	return EINA_FALSE;
}

EAPI void ekeko_ecore_unloopize(void)
{
}

EAPI Eina_Bool ekeko_is_connected(void)
{
	Eix_Transport *t;
	Eix_Transport_State state;

	if (!ekeko_server) return EINA_FALSE;
	/* check the status of the transport */
	t = eix_server_transport_get(ekeko_server);
	state = eix_transport_state_get(t);
	if (state == EIX_TRANSPORT_STATE_CONNECTED)
		return EINA_TRUE;
	return EINA_FALSE;
}

EAPI void ekeko_sync(void)
{
	if (!ekeko_server) return;
	eix_server_sync(ekeko_server);
}
